package com.verisette.ncos.web.master.action;

import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import org.corp.agi.common.annotation.RequiresAuthentication;
import org.corp.agi.common.bean.PagingBean;
import org.corp.agi.service.bean.ServiceRequest;
import org.corp.agi.service.bean.ServiceResponse;
import org.corp.agi.web.action.BaseAction;
import org.corp.agi.web.util.ServiceRequestFactory;
import com.verisette.ncos.common.PrivConst;
import com.verisette.ncos.common.ProjectConstant;
import com.verisette.ncos.persistence.domain.MasterUserGroup;
import com.verisette.ncos.service.master.gateway.UserGroupAuthorizationService;

import com.verisette.ncos.web.master.bean.UserGroupAuthorizationBean;

/**
 * {Description here}
 *
 * @author Thippawan Muangchu (Tip)
 * @since Mar 31, 2010 1:56:41 PM
 * @version 1.0.0
 * @see 
 */

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class UserGroupAuthorizationAction extends BaseAction {
	private Logger logger = Logger.getLogger(UserGroupAuthorizationAction.class);
	
	//initial service
	private UserGroupAuthorizationService userGroupAuthorizationService;
	@Autowired
	@Required
	public void setUserGroupAuthorizationService(UserGroupAuthorizationService userGroupAuthorizationService) {
		this.userGroupAuthorizationService = userGroupAuthorizationService;
	}
	
	//paging variable
	private PagingBean pagingBean;
	
	//initial variable
	private HashMap<String, String> userGroupMap;
	private MasterUserGroup userGroupBean;
	private UserGroupAuthorizationBean bean;
	private boolean isObjListAll;
	
	//input variable from page
	private String userGroupSelected;
	private List<String> checkedId;
	
	//get User Group to combobox
	@RequiresAuthentication(granted={PrivConst.S060100})
	public String init() throws Exception{
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": init" );
		
		//paging
		pagingBean = new PagingBean(ProjectConstant.ROW_PER_PAGE);
		
		//call load user group service
		ServiceRequest serviceRequest = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);		
		ServiceResponse serviceResponse = userGroupAuthorizationService.loadUserGroup(serviceRequest);
		
		//check service response
		if(isServiceResponseSuccess(serviceResponse)){
			userGroupMap = (HashMap<String, String>)serviceResponse.getValueObject();
		}
		
		return "getGroupCombo";
	}
	
	//get Authorization list by group total
	@RequiresAuthentication(granted={PrivConst.S060100})
	public String getAuthListByGroup() throws Exception{
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": getAuthListByGroup" );
		
		//get usgId from page
		String usgId = userGroupSelected;
		
		//validate case user don't select group auth (usgId = 0)
		if( !("0".equals(usgId)) ){
			//set usgId with service request
			ServiceRequest serviceRequest = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);		
			userGroupBean = new MasterUserGroup();
			userGroupBean.setUsgId(Integer.parseInt(usgId));
			serviceRequest.setValueObject(userGroupBean);
			
			//set paging to request
			serviceRequest.setPaging(pagingBean);
			
			//call get authorization list by group total service
			ServiceResponse serviceResponse = userGroupAuthorizationService.getAuthList(serviceRequest);
			
			//check service response
			if(isServiceResponseSuccess(serviceResponse)){
				bean = (UserGroupAuthorizationBean) serviceResponse.getValueObject();
				pagingBean = (PagingBean)serviceResponse.getPaging();
			}
		}
		return "getAuthListByGroup";
	}

	//get checkbox value to insert/update process
	@RequiresAuthentication(granted={PrivConst.S060101})
	public String updateUserGroup() throws Exception{
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": updateUserGroup" );
		
		List<String> checked = checkedId;

		//get usgId from page
		String usgId = userGroupSelected;
		logger.debug("userGroupSelected ==>" + userGroupSelected);
		
		
		ServiceRequest serviceRequest = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
		bean.setChecked(checked);
		bean.setUsgId(Integer.parseInt(usgId));
		serviceRequest.setValueObject(bean);
		
		//call get authorization list by group total service
		ServiceResponse serviceResponse = userGroupAuthorizationService.updateUserGroup(serviceRequest);
		
		//check service response
		if(isServiceResponseSuccess(serviceResponse)){
			//query result and redirect to search result page
			this.getAuthListByGroup();
		}
		
		return "getAuthListByGroup";
	}
	
	
	//getter-setter
	public HashMap<String, String> getUserGroupMap() {
		return userGroupMap;
	}
	public void setUserGroupMap(HashMap<String, String> userGroupMap) {
		this.userGroupMap = userGroupMap;
	}
	public String getUserGroupSelected() {
		return userGroupSelected;
	}
	public void setUserGroupSelected(String userGroupSelected) {
		this.userGroupSelected = userGroupSelected;
	}
	public MasterUserGroup getUserGroupBean() {
		return userGroupBean;
	}
	public void setUserGroupBean(MasterUserGroup userGroupBean) {
		this.userGroupBean = userGroupBean;
	}
	public UserGroupAuthorizationBean getBean() {
		return bean;
	}
	public void setBean(UserGroupAuthorizationBean bean) {
		this.bean = bean;
	}
	public boolean isObjListAll() {
		return isObjListAll;
	}
	public void setObjListAll(boolean isObjListAll) {
		this.isObjListAll = isObjListAll;
	}
	public List<String> getCheckedId() {
		return checkedId;
	}
	public void setCheckedId(List<String> checkedId) {
		this.checkedId = checkedId;
	}
	public PagingBean getPagingBean() {
		return pagingBean;
	}
	public void setPagingBean(PagingBean pagingBean) {
		this.pagingBean = pagingBean;
	}

	
}
